<script setup>
import {
    Edit,
    Delete,
    Close
} from '@element-plus/icons-vue'


import { ref } from 'vue'


const types = ref([
    {
        "id": 1,
        "contentValue": "等级",
    },
    {
        "id": 2,
        "contentValue": "里程碑",
    },
    
])

const type = ref('')




const dataList = ref([


])




import {versionSeasonListService ,versionSeaonAddService,versionSeasonService} from '@/api/versionSeason.js'



import { Plus } from '@element-plus/icons-vue'
import { QuillEditor } from '@vueup/vue-quill'
import '@vueup/vue-quill/dist/vue-quill.snow.css'
//控制抽屉是否显示
const visibleDrawer = ref(false)
//添加表单数据模型
const dataModel = ref({
    type:1
})


//导入token
import { useTokenStore } from '@/stores/token.js';
const tokenStore = useTokenStore();
const queryTime = ref([])


//上传成功的回调函数
const uploadSuccess = (result) => {
  dataModel.value.imgUrl = result.data;
    console.log(result.data);
}

const uploadAudioSuccess = (result) => {
  dataModel.value.audioUrl = result.data;
    console.log(result.data);
}


//添加文章
import {ElMessage, ElMessageBox} from 'element-plus'
import {
  activityAddService,
  activityDelService,
  activityEditService,
  activityListService,
  activityPublishService
} from "@/api/activity.js";
import {treeHoleDelService} from "@/api/treeHole.js";
import {interestTopInterestPublishService} from "@/api/interestPublishRecordData.js";
const addData = async (clickState) => {
    //把发布状态赋值给数据模型
    dataModel.value.state = clickState;

    //调用接口
    let result = await activityAddService(dataModel.value);

    ElMessage.success(result.msg ? result.msg : '添加成功');

    //让抽屉消失
    visibleDrawer.value = false;

    //刷新当前列表
  activityList()
}

//编辑分类
const updateData = async () => {
    //调用接口
    let result = await activityEditService(dataModel.value);

    ElMessage.success(result.msg ? result.msg : '修改成功')

    //调用获取所有分类的函数
  activityList();

    //隐藏弹窗
    visibleDrawer.value = false;
}
type.value=1;


//获取文章列表数据
const activityList = async () => {
    let params = {

      timeFrom: queryTime.value ? queryTime.value[0] : null,
      timeEnd: queryTime.value ? queryTime.value[1] : null,
    }

    let result = await activityListService(params);
   dataList.value = result.data;

}


activityList();



//定义变量,控制标题的展示
const title = ref('')
const showData = (row) => {
    
  visibleDrawer.value = true, title.value = '编辑活动'
  dataModel.value.type = row.type;
  dataModel.value.num = row.num;
  dataModel.value.name = row.name;
  dataModel.value.simpleName = row.simpleName;
  dataModel.value.fromTime=row.fromTime;
  dataModel.value.endTime=row.endTime;
  dataModel.value.id = row.id;


}
const delData = (row) => {
  //提示用户  确认框

  ElMessageBox.confirm(
      '你确认要删除该活动吗?',
      '温馨提示',
      {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
      }
  )
      .then(async () => {
        //调用接口
        let result = await activityDelService(row.id);
        ElMessage({
          type: 'success',
          message: '删除成功',
        })
        //刷新列表
        activityList();
      })
      .catch(() => {
        ElMessage({
          type: 'info',
          message: '用户取消了删除',
        })
      })
}
//清空模型的数据
const clearData = () => {

  dataModel.value.type = 1;
  dataModel.value.num = '';
  dataModel.value.name = '';
  dataModel.value.simpleName = '';
  dataModel.value.fromTime='';
  dataModel.value.endTime='';
  dataModel.value.id = '';
}

const handleChange = (value) => {
   if(dataModel.value.type ==1){
     dataModel.value.name = "第"+value+"版本";
     dataModel.value.simpleName = 'g'+value;
   }else{
     dataModel.value.name = "第"+value+"赛事";
     dataModel.value.simpleName = 's'+value;
   }
}
const publish = async (row) => {

  let result = await activityPublishService(row.id);
  ElMessage.success('置顶成功');
  activityList();

}


</script>
<template>
    <el-card class="page-container">
        <template #header>
            <div class="header">
                <span>活动新增</span>
                <div class="extra">
                    <el-button type="primary" @click="visibleDrawer = true; title = '添加活动'; clearData()">添加活动</el-button>
                </div>
            </div>
        </template>
        <!-- 搜索表单 -->
        <el-form inline>
          <el-form-item label="时间：">
            <el-date-picker v-model="queryTime"  type="daterange" align="right" unlink-panels range-separator="至"
                            start-placeholder="开始日期" end-placeholder="结束日期" value-format="YYYY-MM-DD">
            </el-date-picker>
          </el-form-item>

            <el-form-item>
                <el-button type="primary" @click="activityList">搜索</el-button>
                <el-button @click="queryTime = ''">重置</el-button>
            </el-form-item>
        </el-form>
        <!-- 文章列表 -->
        <el-table :data="dataList" border  style="width: 100%" @selection-change="handleSelectionChange">
        

            <el-table-column label="名称" prop="name"></el-table-column>
            <el-table-column label="开始时间" prop="fromTime"></el-table-column>
            <el-table-column label="结束时间" prop="endTime"></el-table-column>
          <el-table-column label="发布状态"  >
            <template v-slot="scope">
              <span v-if="scope.row.publishState==1">已发布</span>
              <span v-if="scope.row.publishState==0">未发布</span>
              <span v-else ></span>
            </template>
          </el-table-column>
            <el-table-column label="创建时间" width="200px" prop="createTime"></el-table-column>
          <el-table-column label="发布管理"  >
            <template #default="{ row }">
              <el-button  v-if="row.publishState ==0"   type="primary" @click="publish(row)">发布</el-button>
            </template>
          </el-table-column>
            <el-table-column label="操作"  width="150px">
                <template #default="{ row }">
                    <el-button :icon="Edit" circle plain type="primary" @click="showData(row)"></el-button>
                  <el-button :icon="Delete" circle plain type="danger" @click="delData(row)"></el-button>
                </template>
            </el-table-column>
            <template #empty>
                <el-empty description="没有数据" />
            </template>
        </el-table>




        <!-- 抽屉 -->
        <el-drawer v-model="visibleDrawer" :title="title" direction="rtl" size="50%">
            <!-- 添加文章表单 -->
            <el-form :model="dataModel" label-width="100px">



              <el-form-item label="活动名称">
                <el-input v-model="dataModel.name" type="text" style="width: 210px;"/>
              </el-form-item>

                <el-form-item label="开始时间" >
                    <el-date-picker v-model="dataModel.fromTime"  type="date" value-format="YYYY-MM-DD">
                </el-date-picker>
                </el-form-item>
                <el-form-item label="结束时间" >
                    <el-date-picker v-model="dataModel.endTime"  type="date" value-format="YYYY-MM-DD">
                </el-date-picker>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="title == '添加活动' ? addData() : updateData()">保存</el-button>

                </el-form-item>
            </el-form>
        </el-drawer>


    </el-card>
</template>
<style lang="scss" scoped>
.page-container {
    min-height: 100%;
    box-sizing: border-box;

    .header {
        display: flex;
        align-items: center;
        justify-content: space-between;
    }
}


/* 抽屉样式 */
.avatar-uploader {
    :deep() {
        .avatar {
            width: 178px;
            height: 178px;
            display: block;
        }

        .el-upload {
            border: 1px dashed var(--el-border-color);
            border-radius: 6px;
            cursor: pointer;
            position: relative;
            overflow: hidden;
            transition: var(--el-transition-duration-fast);
        }

        .el-upload:hover {
            border-color: var(--el-color-primary);
        }

        .el-icon.avatar-uploader-icon {
            font-size: 28px;
            color: #8c939d;
            width: 178px;
            height: 178px;
            text-align: center;
        }
    }
}

.editor {
    width: 100%;

    :deep(.ql-editor) {
        min-height: 200px;
    }
}
</style>